//'use strict'

const Backend = "localhost/backend"
function BackendPost(msg, func) {

}

function BackendGet(msg, func) {

}

function ChangeImg(elem, img) {
    const frame = document.getElementById(elem);
    frame.setAttribute('src', img);
}

function WebPostJSON(url, json, origin, succFunc, failFunc) {
    let request;
    if (typeof XMLHttpRequest != "undefined")
        request = new XMLHttpRequest();
    else if (typeof ActiveXObject != "undefined")
        request = new ActiveXObject("Microsoft.XMLHTTP");
    else
        alert("browser doesn't support GET function")
    request.open("POST", url, true);
    request.setRequestHeader("Content-type", "application/json");

    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            succFunc(request);
        }
        else {
            failFunc(request)
        }
    }

    request.send(json.toString());
}

function WebPostMsg(url, msg, origin, succFunc, failFunc) {
    let request;
    if (typeof XMLHttpRequest != "undefined")
        request = new XMLHttpRequest();
    else if (typeof ActiveXObject != "undefined")
        request = new ActiveXObject("Microsoft.XMLHTTP");
    else
        alert("browser doesn't support GET function")
    request.open("POST", url, true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    request.withCredentials = true;
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            succFunc(request);
        }
        else {
            failFunc(request)
        }
    }

    request.send(msg);
}

function WebGet(Url, succFunc, failFunc) {
    /*
    let request;
    if (typeof XMLHttpRequest != "undefined")
        request = new XMLHttpRequest();
    else if (typeof ActiveXObject != "undefined")
        request = new ActiveXObject("Microsoft.XMLHTTP");
    else
        alert("browser doesn't support GET function")
    request.open("GET", url, true);
    request.withCredentials = true;

    request.onreadystatechange = function () {
        /
        if (request.readyState === 4 && request.status === 200) {
            succFunc(request);
            alert("??")
        }
        else {
            failFunc(request);
        }/
        console.log('on ready stage:')
        console.log(request.status)
        console.log(request.readyState)
        console.log(request)
        console.log(request.responseText)
    }
    request.send()

     */

    $.cors = $.getJSON(Url, null, succFunc);
    $.crossDomain = true;
}